import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import HomeView from '../views/HomeView.vue'
import UserList from '../views/UserList.vue'
import ItemList from '../views/ItemList.vue'
import {getToken} from '../utils/local.js'
Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'login',
  component: Login
}, {
  path: '/home',
  name: 'home',
  component: HomeView,
  children: [{
    path: '/getUserList',
    name: 'UserList',
    component: UserList
  }, {
    path: '/getItemList',
    name: 'ItemList',
    component: ItemList
  }]
}]

const router = new VueRouter({
  mode: 'history',
  routes
})

router.beforeEach((to, from, next) => {
  if (to.name == 'login') return next()
  let apiKey = localStorage.getItem("apikey")
  if (!apiKey) return next('/login')
  next()
})

export default router
